<?php
/**
 * @author zhangxin
 * @desc 微信支付功能封装
 */
$wxpayLibPath = dirname(__FILE__) . '/../library/ThirdParty/WxPay/';
include_once $wxpayLibPath . '/WxPay.Api.php';
include_once $wxpayLibPath . '/WxPay.Notify.php';
include_once $wxpayLibPath . '/WxPay.NativePay.php';
include_once $wxpayLibPath . '/WxPay.Data.php';

class WxpayModel extends WxPayNotify
{
    public $errno  = 0;
    public $errmsg = "";
    private $_db;

    public function __construct()
    {
        $this->_db = new PDO("mysql:host=127.0.0.1;dbname=imooc;", "root", "123456");
    }

    public function createbill($itemid, $uid)
    {
        $query = $this->_db->prepare("select * from `item` where `id`=?");
        $query->execute([$itemid]);
        $ret = $query->fetchAll();
        if (!$ret || count($ret) != 1) {
            $this->errno  = -6003;
            $this->errmsg = "找不到这件商品";
            return false;
        }
        $item = $ret[0];
        if (strtotime($item['endtime']) <= time()) {

        }

        if (intval($item['stock']) <= 0) {
            $this->errno  = -6005;
            $this->errmsg = "商品库存不够，不能购买";
            return false;
        }

        /**
         * 创建订单
         */
        $query = $this->_db->prepare("insert into `bill` (`itemid`,`uid`,`price`,`status`) VALUES ( ?, ?, ?, 'unpaid') ");
        $ret   = $query->execute();
        if (!$ret) {
            $this->errno  = -6006;
            $this->errmsg = "创建订单失败";
            return false;
        }

        $lastId = intval($this->_db->lastInsertId());

        //库存-1
        $query = $this->_db->prepare("");
        $ret   = $query->execute();
        if (!$ret) {
            $this->errno  = -6007;
            $this->errmsg = "更新库存失败";
            return false;
        }
        return $lastId;
    }

    public function qrcode($billId)
    {
        $query = $this->_db->prepare("select * from `bill` where `id`= ? ");
        $query->execute([$billId]);
        $ret = $query->fetchAll();
        if (!$ret || count($ret) != 1) {
            $this->errno  = -6009;
            $this->errmsg = "找不到账单信息";
            return false;
        }
        $bill  = $ret[0];
        $query = $this->_db->prepare("select * from `item` where `id`= ? ");
        $query->execute();
        $ret = $query->fetchAll();
        if (!$ret || count($ret) != 1) {
            $this->errno  = -6010;
            $this->errmsg = "找不到商品信息";
            return false;
        }
        $item = $ret[0];

        $input = new WxPayUnifiedOrder();
        $input->SetBody($item['name']);
        $input->SetAttach($billId);
        $input->SetOut_trade_no(WxPayConfig::MCHID . date("YmdHis"));
        $input->SetTotal_fee($bill['price']);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 86400 * 3));
        $input->SetGoods_tag($item['name']);
        $input->SetNotify_url("");
        $input->SetTrade_type("NATIVE");
        $input->SetProduct_id($billId);

        $notify = new NativePay();
        $result = $notify->GetPayUrl($input);
        $url    = $result["code_url"];
        return $url;

    }

    public function callback()
    {
        $xmlData = file_get_contents("php://input");
        if (substr_count($xmlData, "<result_code><![CDATA[SUCCESS]]></result_code>") == 1 &&
            substr_count($xmlData, "<return_code><![CDATA[SUCCESS]]></return_code>") == 1) {
            preg_match('/<attach>(.*)\[(\d+)\](.*)<\/attach>/i', $xmlData, $match);
            if (isset($match[2]) && is_numeric($match[2])) {
                $billId = intval($match[2]);
            }
            preg_match('/<transaction_id>(.*)\[(\d+)\](.*)<\/transaction_id>/i', $xmlData, $match);
            if (isset($match[2]) && is_numeric($match[2])) {
                $transactionId = intval($match[2]);
            }
        }

        if (isset($billId) && isset($transactionId)) {
            $query = $this->_db->prepare("update `bill` set `transaction`=? ,`ptime`=? ,`status`='paid' where `id`=? ");
            $query->execute(array($transactionId, date("Y-m-d H:i:s"), $billId));
        }

    }
}
